﻿// CRenderObject.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Casters.source.Animation;
#endregion

namespace Casters.source.Objects
{
    public class CRenderObject : CBaseObject
    {
        // Variables

        public Texture2D m_Texture;
        
        public int m_nTextureID;
        public Vector2 m_vTileSizePixels;
        public Vector2 m_vNumTiles;
        
        public Rectangle m_rSourceRect;

        // Animation stuff
        protected CAnimationPlayer m_AnimPlayer = null;
        protected CAnimation[] m_AnimList;
        
        public CRenderObject()
        {
            m_vPosition = Vector2.Zero;
            m_vTileSizePixels = Vector2.Zero;
            m_vNumTiles = Vector2.Zero;
            m_rSourceRect = new Rectangle();
            m_nTextureID = -1;
        }

        /// <summary>
        /// Setups up the baseline parameters for rendering an object
        /// </summary>
        /// <param name="_nTextureID">ID for position on sprite sheet as if it were a 1d array.  Column Based</param>
        /// <param name="_Texture">Texture2D held in - Subject to change</param>
        /// <param name="_vTextureSize">Vector2 containing the size of an indiviual item off a sprite sheet in pixels</param>
        /// <param name="_vPos">Position to be rendered - Player: x and y position on tilemap, Effect: x and y position</param>
        public void SetupRenderObject(int _nTextureID, Texture2D _Texture, Vector2 _vPos, Vector2 _vNumTiles, Vector2 _vTileSizePixels, string animation)
        {
            m_Texture = _Texture;
            m_vPosition = _vPos;
            m_nTextureID = _nTextureID;
            m_vTileSizePixels = _vTileSizePixels;
            m_vNumTiles = _vNumTiles;

            if (animation != "none")
            {
                m_AnimPlayer = new CAnimationPlayer(this);
                m_AnimList = m_AnimPlayer.LoadAnimation(animation);
                m_AnimPlayer.SetAnimation(m_AnimList[0]);
            }
        }

        /// <summary>
        /// GetTileTex
        /// </summary>
        /// <param name="_ID">An ID value for the appropriate texture on the tilesheet</param>
        /// <returns>Rectangle that is the source rect for rendering</returns>
        public Rectangle GetSpriteTex(int _ID)
        {
            // Determine cell position
            int X = _ID / (int)m_vNumTiles.X;
            int Y = _ID % (int)m_vNumTiles.Y;

            // Return the appropriate RECT
            Rectangle toReturn = new Rectangle((int)m_vTileSizePixels.X * X, (int)m_vTileSizePixels.Y * Y,
                                               (int)m_vTileSizePixels.X, (int)m_vTileSizePixels.Y);

            return toReturn;
        }

        /// <summary>
        /// Placeholder update function
        /// </summary>
        /// <param name="dt">Delta Time</param>
        public override void Update(float dt)
        {
            if(m_AnimPlayer != null)
                m_AnimPlayer.Update(dt);
        }

        /// <summary>
        /// Draw Function - Rewrite this in children if planning on using animation
        /// </summary>
        public override void Draw()
        {
            if (m_AnimPlayer != null)
                m_AnimPlayer.Draw();
            else
            {
                // Scales objects based on camera perspective
                float zoom = CGame.GetInstance().m_Camera.m_fZoom;
                CRenderEngine.Draw(m_Texture, m_vPosition * zoom, GetSpriteTex(m_nTextureID), Color.White, 0f, Vector2.Zero, zoom, SpriteEffects.None, 0f);
            }
        }
    }
}
